package file.untils;

import file.constant.SysConstant;
import file.model.FileModel;

import javax.swing.*;
import javax.swing.filechooser.FileSystemView;
import javax.swing.plaf.basic.BasicTreeUI;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeCellRenderer;
import javax.swing.tree.MutableTreeNode;
import javax.swing.tree.TreeNode;
import java.io.File;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 文件的操作
 * @Author: zou
 * @email: 704572528@qq.com
 * @Date: 2020/10/27 15:19
 */
public class FileUntils {


    /**
     * 获取本机的所有盘符以及描述
     * @return
     */
    public static List<FileModel> listDrive() {

        List<FileModel> fileModels = new ArrayList<>();
        File[] files = File.listRoots();
        FileModel fm = null;
        // 获取系统
        FileSystemView sys = FileSystemView.getFileSystemView();
        for (File file : files) {
            fm = new FileModel();
            fm.setRoot(true);
            fm.setFolder(true);
            fm.setName(sys.getSystemTypeDescription(file));
            fm.setPath(file.getPath());
            fileModels.add(fm);
        }
        return fileModels;
    }

    /**
     * 根据根目录取所有文件夹
     * @param fileModel
     * @param root
     * @param leftTree
     * @return
     */
    public static List<FileModel> listRootFile(FileModel fileModel, DefaultMutableTreeNode root){

        List<FileModel> fileModels = new ArrayList<>();
        // 是根目录下
        if (fileModel.getRoot()){

            File file = new File(fileModel.getPath());

            getFileFolder(fileModels,file ,root);

        }
        return fileModels;
    }

    /**
     * 获取文件夹
     * @param fileModels
     * @param file
     * @param root
     */
    private static void getFileFolder(List<FileModel> fileModels, File file, DefaultMutableTreeNode root) {

        if (file != null) {

            File[] files = file.listFiles();
            if (files != null) {
                for (File fl : files) {
                    // 文件夹的话
                    if (!fl.isFile()){
                        if (!fl.isHidden()) {
                            DefaultMutableTreeNode thisTree = new DefaultMutableTreeNode(fl.getName(), true);
                            FileModel fileModel = getFileModel(fl);
                            thisTree.setUserObject(fileModel);
                            root.add(thisTree);
                            fileModels.add(fileModel);
                        }
                        // TODO 递归的优化
                        // getFileFolder(fileModels,fl, A);
                    }
                }
            }
        }
    }

    public static FileModel getFileModel(File fl) {
        FileModel fm = new FileModel();
        fm.setRoot(false);
        fm.setFolder(fl.isDirectory());
        fm.setName(fl.getName());
        fm.setPath(fl.getPath());
        fm.setSize(getFileSize(fl));
        fm.setType(getType(fl));
        Date date = new Date(fl.lastModified());
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        fm.setChangeDate(sdf.format(date));
        return fm;
    }

    /**
     * 获取文件类型
     * @param fl
     * @return
     */
    private static String getType(File fl) {
        String name = fl.getName();
        if (fl.isDirectory() || name == null || name.lastIndexOf(".")== -1 ) {
            return "";
        }
        return name.substring(name.lastIndexOf("."),name.length());
    }

    /**
     * 转换各大大小分
     * mb kb m G
     * @param fl
     * @return
     */
    private static String getFileSize(File fl) {
        long length = fl.length();
        DecimalFormat df = new DecimalFormat("#.00");
        String strSize = "";
        if (length == 0) {
            return "";
        }
        if (length < 1024) {
            strSize = df.format((double) length) + "B";
        } else if (length < 1048576) {
            strSize = df.format((double) length / 1024) + "KB";
        } else if (length < 1073741824) {
            strSize = df.format((double) length / 1048576) + "MB";
        } else {
            strSize = df.format((double) length / 1073741824) + "GB";
        }
        return strSize;
    }


}
